package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.NursingProjectMapper;
import com.itheima.pojo.NursingProject;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.ProjectParam;
import com.itheima.service.NursingProjectService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Override
    public PageResult getAll(ProjectParam projectParam) {
        //封装分页信息
        IPage<NursingProject> page = new Page<>(projectParam.getPage(),projectParam.getPageSize());
        //封装查询条件
        QueryWrapper<NursingProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(projectParam.getName()),"name",projectParam.getName())
                .like(ObjectUtils.isNotEmpty(projectParam.getStatus()),"status",projectParam.getStatus());
        //分页查询
        IPage<NursingProject> pageInfo = nursingProjectMapper.selectPage(page, queryWrapper);
        return new PageResult(pageInfo.getTotal(),pageInfo.getRecords());
    }

    @Override
    public void addProject(NursingProject nursingProject) {
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProject getProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    @Override
    public void updateProject(NursingProject nursingProject) {
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.updateById(nursingProject);
    }

    @Override
    public void deleteProject(Long id) {
        nursingProjectMapper.deleteById(id);
    }
}
